package com.lngsyz.file.biz.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.lngsyz.file.service.source1.SystemFileuploadService;
import com.lngsyz.file.util.CommonUtil;

/**
 * 
 *	文件信息相关接口
 */
@Service
public class BizFileService {
	
	@Autowired
	SystemFileuploadService systemFileuploadService;
	@Autowired
	TransFileService transFileService;
	@Autowired
	DelFileService delFileService;
	
	// 上传文件
	@Transactional(rollbackFor = Exception.class)
	public String upload(MultipartFile multipartFile) throws Exception {
		return systemFileuploadService.insert(multipartFile);
	}
	
	// 按指定路径，上传文件
	@Transactional(rollbackFor = Exception.class)
	public void uploadWithPath(MultipartFile multipartFile, String path) throws Exception {
		systemFileuploadService.insertWithPath(multipartFile, path);
	}
	
	// 获取文件
	public void getFileStream(String id, HttpServletResponse response) throws Exception {
		systemFileuploadService.getFileStream(id, response);
	}
	
	// 获取文件
	public void getFileStreamByAccessUrl(String accessUrl, String fileName, HttpServletResponse response) throws Exception {
		systemFileuploadService.getFileStreamByAccessUrl(accessUrl, fileName, response);
	}
	
	public void transFileJob(String startDate, int days){
		int jobCount = 30;
//		String startDate = "2015-08-30";
		for(int i = 1; i <= days; i++) {
			// 执行的日期
			String executeDate = CommonUtil.addDays(startDate, i-1);
			System.out.println("当前查询：" + executeDate + "\t 当前时间：" + getDate() + "\t 设定的任务（起始日期：" + startDate + "\t 截止日期：" + CommonUtil.addDays(startDate, days-1) + "）");
			transFileService.transFile(executeDate, jobCount);
			System.out.println("");
			System.out.println("=====================================" + getDate() + "结束" + executeDate + "==========================================");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(getDate() + "\t 设定的任务（起始日期：" + startDate + "\t 截止日期：" + CommonUtil.addDays(startDate, days-1) + "）已结束======");
    }
	
	public void delFileJob(String startDate, int days){
		int jobCount = 30;
//		String startDate = "2015-08-30";
		for(int i = 1; i <= days; i++) {
			// 执行的日期
			String executeDate = CommonUtil.addDays(startDate, i-1);
			System.out.println("当前查询：" + executeDate + "\t 当前时间：" + getDate() + "\t 设定的任务（起始日期：" + startDate + "\t 截止日期：" + CommonUtil.addDays(startDate, days-1) + "）");
			delFileService.delFile(executeDate, jobCount);
			System.out.println("");
			System.out.println("=====================================" + getDate() + "结束" + executeDate + "==========================================");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println(getDate() + "\t 设定的任务（起始日期：" + startDate + "\t 截止日期：" + CommonUtil.addDays(startDate, days-1) + "）已结束======");
	}

    private String getDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }
}
